package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import dao.SuppDao;
import entity.Supp;
import util.DBUtils;

public class SuppDaoImpl implements SuppDao {
/*
 * suppid varchar(15) primary key, -- 供应商流水号
  suppname varchar(200) not null, -- 供应商名
  supparea varchar(200) not null, -- 供应商地址
  contact varchar(20) not null,   -- 联系人
  mobile varchar(50) not null,    -- 联系电话
  evalevel varchar(2) 
 */
	@Override
	public boolean insert(Supp supp) {
		try (Connection c = DBUtils.getConnection()){
			String sql = "insert into supp values(?,?,?,?,?,?)";
			PreparedStatement ps = c.prepareStatement(sql);
			//设置参数
			ps.setString(1, supp.getSuppid());
			ps.setString(2, supp.getSuppname());
			ps.setString(3, supp.getSupparea());
			ps.setString(4, supp.getContact());
			ps.setString(5, supp.getMobile());
			ps.setString(6, supp.getEvalevel());
			//执行
			int row = ps.executeUpdate();
			return row == 1 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean updateLevelById(String suppid, String evalevel) {
		try (Connection c = DBUtils.getConnection()){
			String sql = "update supp set evalevel=? where suppid=?";
			PreparedStatement ps = c.prepareStatement(sql);
			//设置参数
			ps.setString(1, evalevel);
			ps.setString(2, suppid);
			//执行
			int row = ps.executeUpdate();
			return row == 1 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

}
